package payment.mobilepos.unionpay.com.demo;

import android.Manifest;
import android.app.Activity;
import android.content.pm.PackageManager;
import android.os.Build;
import android.support.v4.app.ActivityCompat;


public class PermissionUtil {

    /**
     * Checks if the app has permission to write to device storage
     * If the app does not has permission then the user will be prompted to
     * grant permissions
     *
     * @param activity
     */
    public static void verifyStoragePermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Check if we have write permission
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission
                    .WRITE_EXTERNAL_STORAGE);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // We don't have permission so prompt the user
                String[] storagePermissions = {Manifest.permission.READ_EXTERNAL_STORAGE,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE};
                ActivityCompat.requestPermissions(activity, storagePermissions, PermissionRequest
                        .EXTERNAL_STORAGE);
            }
        }
    }

    public static void verifyCameraPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Check if we have write permission
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission
                    .CAMERA);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // We don't have permission so prompt the user
                String[] cameraPermissions = {Manifest.permission.CAMERA};
                ActivityCompat.requestPermissions(activity, cameraPermissions, PermissionRequest
                        .CAMERA);
            }
        }
    }

    public static void verifyRecodeAudioPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Check if we have write permission
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission
                    .RECORD_AUDIO);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // We don't have permission so prompt the user
                String[] cameraPermissions = {Manifest.permission.RECORD_AUDIO};
                ActivityCompat.requestPermissions(activity, cameraPermissions, PermissionRequest
                        .CAMERA);
            }
        }
    }

    public static void verifyNfcPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Check if we have write permission
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.NFC);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                // We don't have permission so prompt the user
                String[] nfcPermissions = {Manifest.permission.NFC};
                ActivityCompat.requestPermissions(activity, nfcPermissions, PermissionRequest.NFC);
            }
        }
    }

    public static void verifyWriteSettingsPermissions(Activity activity) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            // Android M Permission check
            int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission
                    .WRITE_SETTINGS);
            if (permission != PackageManager.PERMISSION_GRANTED) {
                String[] writeSettingsPermissions = {Manifest.permission.WRITE_SETTINGS};
                ActivityCompat.requestPermissions(activity, writeSettingsPermissions,
                        PermissionRequest.WRITE_SETTINGS);
            }
        }
    }

    public static class PermissionRequest {
        // permission: EXTERNAL_STORAGE
        public final static int EXTERNAL_STORAGE = 1;

        // permission: camera
        public final static int CAMERA = 2;

        // permission: NFC
        public final static int NFC = 3;

        // permission: WRITE_SETTINGS
        public final static int WRITE_SETTINGS = 4;

        // permission: WRITE_SETTINGS
        public final static int RECODE_AUDIO = 4;
    }

}

